import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JOptionPane;


public class Client implements Runnable, Connector {

	
	private Game game;
	private Socket server = null;
	private Scanner in = null;
	private PrintWriter out = null;
	
	
	public Client(Game game) {
		this.game = game;
		game.getGf().getMousePanel().setListenerBlocked(true);
		try {
			server = new Socket("localhost", 5291);
			in = new Scanner(server.getInputStream());
			out = new PrintWriter(server.getOutputStream());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null,"Es konnte keine Server gefunden werden.",
					"Verbindungsproblem", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null,"Es konnte keine Verbindung  hergestellt werden.",
					"Verbindungsproblem", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	@Override
	public void sendAction(int i) {
		out.print(i);
	}

	@Override
	public void run() {
		int i = in.nextInt(-1);
		if (i != -1){
			game.dropStone(i);
		}
		game.getGf().getMousePanel().setListenerBlocked(false);
	}

}
